package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


/**
  异步编排
 */
@Controller
public class  WebSkuDetailController {
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor myThreadPool;
    //编写访问的控制器！
    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable("skuId")Long skuId, Model model){
        //根据 skuId查询商品的实时价格
        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(()-> {
                BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            System.out.println(Thread.currentThread().getName());
                model.addAttribute("price", skuPrice);
        },myThreadPool);
        //根据skuId查询sku的基本信息
        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(()-> {
                SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            System.out.println(Thread.currentThread().getName());

            model.addAttribute("skuInfo", skuInfo);
                return skuInfo;
        },myThreadPool);
        //根据分类category3Id查询 商品的分类信息
        CompletableFuture<Void> categoryViewFuture=skuInfoFuture.thenAcceptAsync((SkuInfo skuInfo)-> {
                Long category3Id = skuInfo.getCategory3Id();
                BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
            System.out.println(Thread.currentThread().getName());

            model.addAttribute("categoryView",categoryView);
        },myThreadPool);
        //销售属性组合id 和skuId的对应关系
        CompletableFuture<Void> salePropertyValueIdJsonFuture=skuInfoFuture.thenAcceptAsync((SkuInfo skuInfo)-> {
                 Long productId = skuInfo.getProductId();
                 Map<Object, Object> salePropertyIdAndSkuIdMapping = productFeignClient.getSalePropertyIdAndSkuIdMapping(productId);
            System.out.println(Thread.currentThread().getName());

            model.addAttribute("salePropertyValueIdJson", JSON.toJSONString(salePropertyIdAndSkuIdMapping));
         },myThreadPool);
        //获取所有销售属性 spu和一份sku 对应的销售属性
        CompletableFuture<Void> spuSalePropertyList=skuInfoFuture.thenAcceptAsync((SkuInfo skuInfo)-> {
                 Long productId = skuInfo.getProductId();
                 List<ProductSalePropertyKey> spuSalePropertyAndSelected = productFeignClient.getSpuSalePropertyAndSelected(productId, skuId);
            System.out.println(Thread.currentThread().getName());

            model.addAttribute("spuSalePropertyList",spuSalePropertyAndSelected);
         },myThreadPool);
        CompletableFuture.allOf(priceFuture, skuInfoFuture, categoryViewFuture, salePropertyValueIdJsonFuture, spuSalePropertyList).join();
        return "detail/index";
    }
}
